package com.siao.mvc
{
	import com.siao.mvc.core.IContext;
	import com.siao.mvc.core.IMember;
	import com.siao.mvc.core.IObserver;
	import com.siao.mvc.managers.NoticeManager;
	import com.siao.mvc.notice.Notifier;
	import com.siao.mvc.notice.Observer;
	
	/**
	 * 控制器
	 * @author SiaoLeon
	 */
	public class Controller extends Notifier implements IMember
	{
		// -------------------------------------------------------------------------------------------------------- 静态常量
		
		// -------------------------------------------------------------------------------------------------------- 静态变量
		
		// -------------------------------------------------------------------------------------------------------- 静态方法
		
		// -------------------------------------------------------------------------------------------------------- 静态函数
		
		// -------------------------------------------------------------------------------------------------------- 属性
		
		internal var _context:IContext;
		
		public function get context():IContext
		{
			return this._context;
		}
		
		// -------------------------------------------------------------------------------------------------------- 变量
		
		// -------------------------------------------------------------------------------------------------------- 构造
		
		// -------------------------------------------------------------------------------------------------------- 方法
		
		public function onAdd():void
		{
		}
		
		public function onRemove():void
		{
		}
		
		/**
		 * 注册消息回调方法
		 * @param method 注册对象
		 * @param callback 回调方法
		 * @param priority 优先级
		 */
		public function register(method:Object, callback:Function, priority:uint=0):void
		{
			var observer:IObserver = new Observer(this, callback, priority);
			NoticeManager.instance.registerObserver(method, observer);
		}
		
		/**
		 * 移除消息回调方法
		 * @param method 注册对象
		 * @param callback 回调方法
		 */
		public function remove(method:Object, callback:Function):void
		{
			var observers:Array = NoticeManager.instance.observers.getValue(method);
			
			for (var i:int = observers.length - 1; i >= 0; i--)
			{
				var observer:IObserver = observers[i];
				
				if (observer.callback == callback)
				{
					NoticeManager.instance.removeObserver(method, observer);
				}
			}
		}
		
		/**
		 * 清除所有注册的回调方法
		 */
		public function clear():void
		{
			var methods:Array = NoticeManager.instance.observers.keys;
			
			for each (var method:Object in methods)
			{
				var observers:Array = NoticeManager.instance.observers.getValue(method);
				
				for (var i:int = observers.length - 1; i >= 0; i--)
				{
					var observer:IObserver = observers[i];
					
					if (observer.object == this)
					{
						NoticeManager.instance.removeObserver(method, observer);
					}
				}
			}
		}
	
		// -------------------------------------------------------------------------------------------------------- 函数
	
	}
}
